# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymysql
import redis


class MyspiderPipeline(object):
    def process_item(self, item, spider):
        return item


class JingDongpopeline(object):

    def __init__(self):
        # 建立连接
        self.conn = pymysql.connect("localhost", "root", "123456", "spider")
        # 自动自动提交
        self.conn.autocommit(1)

        self.r = redis.Redis(host="node2", port="6379", db=1)

    def process_item(self, item, spider):
        print("JingDongpopeline")
        print(item)

        # 数据写入外部数据库
        # 数据写入mysql

        content = item["content"]
        id = item["id"]
        score = item["score"]
        nickname = item["nickname"]
        userLevelName = item["userLevelName"]
        userClientShow = item["userClientShow"]
        referenceName = item["referenceName"]

        sql = "insert into comment_db values(%s,%s,%s,%s,%s,%s,%s)"

        cursor = self.conn.cursor()
        # 执行sql
        cursor.execute(sql, (id, content, score, nickname, userLevelName, userClientShow, referenceName))

        # 手动提交
        # conn.commit()
        # conn.close()

        # 数据写入redis
        self.r.hset(id, "content", content)
        self.r.hset(id, "score", score)
        self.r.hset(id, "nickname", nickname)
        self.r.hset(id, "userLevelName", userLevelName)
        self.r.hset(id, "userClientShow", userClientShow)
        self.r.hset(id, "referenceName", referenceName)
